#include "TScreenshotSetting.h"

#include "TConfig.h"
#include "TContext.h"

#include "TMessageBox.h"
#include <QApplication>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QCheckBox>
#include <QUuid>
#include <QDir>

#include <QDebug>

TScreenshotUi::TScreenshotUi(QWidget *parent)
    :QWidget(parent)
{
    verticalLayout = new QVBoxLayout(this);
    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    verticalLayout->setContentsMargins(30, 20, 10, 10);
    ckbIsMinisize = new QCheckBox(this);
    ckbIsMinisize->setObjectName(QString::fromUtf8("checkBox"));
    ckbIsMinisize->setMinimumSize(QSize(0, 25));

    verticalLayout->addWidget(ckbIsMinisize);

    ckbRemember = new QCheckBox(this);
    ckbRemember->setObjectName(QString::fromUtf8("checkBox_2"));
    ckbRemember->setMinimumSize(QSize(0, 25));

    verticalLayout->addWidget(ckbRemember);

    horizontalLayout = new QHBoxLayout();
    horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
    horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Expanding);

    horizontalLayout->addItem(horizontalSpacer);

    pbtStart = new QPushButton(this);
    pbtStart->setObjectName(QString::fromUtf8("pushButton"));
    pbtStart->setMinimumSize(QSize(0, 25));

    horizontalLayout->addWidget(pbtStart);

    verticalLayout->addLayout(horizontalLayout);

    ckbIsMinisize->setText(QApplication::translate("Form", "\346\210\252\345\233\276\346\227\266\346\234\200\345\260\217\345\214\226\350\275\257\344\273\266\347\252\227\344\275\223", 0, QApplication::UnicodeUTF8));
    ckbRemember->setText(QApplication::translate("Form", "\350\256\260\344\275\217\350\277\231\344\270\252\350\256\276\347\275\256", 0, QApplication::UnicodeUTF8));
    pbtStart->setText(QApplication::translate("Form", "\345\274\200\345\247\213\346\210\252\345\233\276", 0, QApplication::UnicodeUTF8));

}

TScreenshotSetting::TScreenshotSetting(QWidget *parent)
    : TSkinDialog(parent)
{
    ui = new TScreenshotUi(getClient());
    QVBoxLayout* layout = new QVBoxLayout(getClient());
    layout->setContentsMargins(0,0,0,0);
    layout->addWidget(ui);

    bool isMinisize = TConfig::instance()->isScreenshotMinisize();
    if(isMinisize)
    {
        ui->ckbIsMinisize->setChecked(true);
    }

    connect(ui->pbtStart, SIGNAL(clicked()), this, SLOT(onScreenshotBt()));

    resize(300, 180);
}

void TScreenshotSetting::onScreenshotBt()
{
    if(ui->ckbRemember->isChecked())
    {
        TConfig::instance()->setIsScreenshotMinisize(ui->ckbIsMinisize->isChecked());
        TConfig::instance()->save();
    }

    this->accept();
}

bool TScreenshotSetting::isMinimizeScreen()
{
    return ui->ckbIsMinisize->isChecked();
}


/// --------------------------------  TScreenshotUpload   ------------------------
TScreenshotUpload::TScreenshotUpload(QPixmap *pixmap, QWidget *parent)
    :TSkinDialog(parent)
{
    _pixmap = pixmap;
    ui = new Ui::TScreenshotUpload();
    ui->setupUi(getClient());

    /// upload requester
    _requester = TContext::instance()->requesterFactory()->createAdvUploadRequester();
    connect(_requester.data(), SIGNAL(uploadFinished(bool, TDocumentPointer)),
            this, SLOT(onUploadFinished(bool,TDocumentPointer)));

    connect(ui->pbtUpload, SIGNAL(clicked()), this, SLOT(onUploadClicked()));
}

void TScreenshotUpload::onUploadClicked()
{
    if(ui->ledTitle->text().isEmpty())
    {
        TMessageBox dialog(tr("caution"),
                           tr("Title is empty."),
                           QMessageBox::Ok,
                           QMessageBox::NoButton, this);
        dialog.centerWindow();
        dialog.exec();
        return;
    }

    bool error = false;
    QString tmpDir = TConfig::instance()->tmpDir();
    QDir dir(tmpDir);
    if(!dir.exists())
    {
        dir.cdUp();

        if(!dir.mkdir(tmpDir))
        {
            error = true;
        }
        else
        {
            dir.cd(tmpDir);
        }
    }

    if(!error && dir.exists())
    {
        _filename = dir.absolutePath() + QDir::separator()
                + QUuid::createUuid() + ".jpg";
        qDebug() << "Lee: " + _filename;
        error = !_pixmap->save(_filename, "JPG");
    }

    if(error) /// todo messagebox
    {
        TMessageBox dialog(tr("caution"),
                           tr("can't save the tmpfile. is zhe disk full?"),
                           QMessageBox::Ok,
                           QMessageBox::NoButton, this);
        dialog.centerWindow();
        dialog.exec();
    }
    else  /// uplaod
    {
        _requester->upload(_filename, ui->ledTitle->text(), \
                           ui->cmbShare->currentIndex() == 0 ? false : true);
        this->setEnabled(false);
    }
}

void TScreenshotUpload::onUploadFinished(bool error, TDocumentPointer document)
{
    this->setEnabled(true);
    this->update();

    QFile tmpFile(_filename);
    if(tmpFile.exists())
    {
        tmpFile.remove();
    }

    if(error)
    {
        TMessageBox dialog(tr("caution"),
                           tr("upload error."),
                           QMessageBox::Ok,
                           QMessageBox::NoButton, this);
        dialog.centerWindow();
        dialog.exec();
    }
    else
    {
        TMessageBox dialog(tr("caution"),
                           tr("upload success."),
                           QMessageBox::Ok,
                           QMessageBox::NoButton, this);
        dialog.centerWindow();
        dialog.exec();
    }

    this->accept();
}

